﻿using Microsoft.Extensions.Configuration.Json;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.Json;
using AutomaticPush.Models;
using AutomaticPush.Extension;

namespace AutoPushExt.Common
{
    public class AppConfig
    {
        public static IConfigurationRoot Configuration { get; set; }

        public static Configs configs;
        static AppConfig()
        {
            Configuration = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true).Build();
            configs = Configuration.Get<Configs>();
        }

        /// <summary>
        /// 获取配置
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetConfig(string key)
        {
            string value = Configuration.GetSection(key).Value;
            if (value.IsNullOrEmpty())
                value = "";
            return value;
        }


        public static string GetOdbcConString(string name)
        { 
            var odbc = configs.ODBCSetting.Where(x => x.Name == name).FirstOrDefault();
            var dsn = $"DSN={odbc.DSN};";
            if (!odbc.User.IsNullOrEmpty() && !odbc.PassWord.IsNullOrEmpty())
                dsn = $"{dsn}UID={odbc.User};PWD={odbc.PassWord};";
            return dsn;
        }
    }
}
